<?
class Biz_Common{
    //错误返回信息
    function getURL($type = 'team',$id = null,$attr2 = null){
        switch ($type){
            case 'team':return sprintf('team.php?teamid=%s',$id);break;
            case 'team_schedule':return sprintf('team.php?teamid=%s&type=S&season=%s',$id,$attr2);break;
//            case 'gameinfo':return sprintf('/gameinfo.php?season=%s&gameid=%s',$attr2,$id);break;
            case 'gameinfo':return self::getGameinfoURL($id);break;
            case 'player':return sprintf('/player.php?regnum=%s&season=%s',$id,$attr2);break;
            case 'player_schedule':return sprintf('/player.php?regnum=%s&type=S&season=%s',$id,$attr2);break;
            default:break;
        }
    }
    //获取比赛的URL
    function getGameinfoURL($id){
    	return sprintf('/gameinfo.php?gameid=%s',$id);
    }
    //错误返回信息
    function errorExit($alert_msg,$location = null,$msg = null){
        echo $msg;
        echo "<script>";
        !empty($alert_msg) && print(sprintf("alert('%s');",$alert_msg));
        !empty($location) && print(sprintf("window.location='%s';",$location));
        echo "</script>";
        exit();
    }
    //去掉version等
    function formatURL($url,$stick = 'version'){
        $now_url = preg_replace("/[&]{0,1}$stick=[^&?].*/si",'',$url);
        if(!strstr($now_url,'?')){
            $now_url .= "?";
        }
        return $now_url;
    }
    //获得当前最新赛季
    function getLastSeason($force = false){
        $cache_key = "zc_last_season";
        if($force === true) $season = Lib_Cache::get($cache_key);
        if(!$season){
            $common = new Data_Common();
            $res = $common->getLastSeason();
            if(intval($res['result'][0]['season']) >0){
                $season = $res['result'][0]['season'];
                Lib_Cache::set($cache_key,$season);
            }
        }
        return $season;
    }

    //获得当前最新轮次
    function getLastRound2($season,$force = false){
        $cache_key	= 'ZC_LastRound2'.$season;
        if($force === true) $data = Lib_Cache::get($cache_key);
        if(!$data){
            $common = new Data_Common();
            $res = $common->getLastRound2($season);
            //            print_z($res);
            if(intval($res['result'][0]['round']) >0){
                $data = $res['result'][0]['round'];
                Lib_Cache::set($cache_key,$data);
            }
        }
        return $data;
    }
    //获得当前最新轮次
    function getLastRound($season,$force = false){
        $cache_key	= 'ZC_LastRound'.$season;
        if($force === true) $data = Lib_Cache::get($cache_key);
        if(!$data){
            $common = new Data_Common();
            $res = $common->getLastRound($season);
            if(intval($res['result'][0]['round']) >0){
                $data = $res['result'][0]['round'];
                Lib_Cache::set($cache_key,$data);
            }
        }
        return $data;
    }
    //获得全部赛季列表
    function getSeasonArray(){
        $season = getLastSeason();
        $seasonArray = array();
        for($i=2004;$i<=$season;$i++){
            array_push($seasonArray,$i);
        }
        return $seasonArray;
    }
}
?>